package com.pinapel.aixml.ui.menuBar;

import java.awt.event.ActionEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

import com.pinapel.aixml.controller.Menu.IMenuEditController;
import com.pinapel.aixml.tools.i18n.I18n;
import com.pinapel.aixml.ui.actions.SearchText;
import com.pinapel.aixml.ui.actions.Preferences;

public class MenuEdit extends JMenu{
	
    private javax.swing.JMenuItem jMenuEditCut;
    private javax.swing.JMenuItem jMenuEditCopy;
    private javax.swing.JMenuItem jMenuEditFindReplace;
    private javax.swing.JMenuItem jMenuEditUndo;
    private javax.swing.JMenuItem jMenuEditRedo;
    private javax.swing.JMenuItem jMenuEditPaste;
    private javax.swing.JMenuItem jMenuEditPreferences;
    
    private IMenuEditController mController;

 
    
    public MenuEdit(){
        jMenuEditFindReplace = new javax.swing.JMenuItem();
        jMenuEditFindReplace.setText(I18n.getString(I18n.UI007_FIND_REPLACE));

        jMenuEditUndo = new javax.swing.JMenuItem();
        jMenuEditUndo.setText(I18n.getString(I18n.UI010_UNDO));

        jMenuEditRedo = new javax.swing.JMenuItem();
        jMenuEditRedo.setText(I18n.getString(I18n.UI011_REDO));

        jMenuEditCut = new javax.swing.JMenuItem();
        jMenuEditCut.setText(I18n.getString(I18n.UI012_CUT));

        jMenuEditCopy = new javax.swing.JMenuItem();
        jMenuEditCopy.setText(I18n.getString(I18n.UI013_COPY));

        jMenuEditPaste = new javax.swing.JMenuItem();
        jMenuEditPaste.setText(I18n.getString(I18n.UI014_PASTE));
        
        jMenuEditPreferences = new javax.swing.JMenuItem();
        jMenuEditPreferences.setText(I18n.getString(I18n.UI016_PREFERENCES));
        
        this.setText(I18n.getString(I18n.UI015_EDIT));

        this.add(jMenuEditFindReplace);
        this.add(jMenuEditUndo);
        this.add(jMenuEditRedo);
        this.add(jMenuEditCut);
        this.add(jMenuEditCopy);
        this.add(jMenuEditPaste);
        this.add(jMenuEditPreferences);
        
        jMenuEditUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditUndoActionPerformed(evt);
            }
        });
        jMenuEditRedo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditRedoActionPerformed(evt);
            }
        });
        jMenuEditCut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditCutActionPerformed(evt);
            }
        });
        jMenuEditCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditCopyActionPerformed(evt);
            }
        });
        jMenuEditPaste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditPasteActionPerformed(evt);
            }
        });
        jMenuEditPreferences.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuEditPreferencesActionPerformed(evt);
            }
        });

        
        jMenuEditFindReplace.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuFileSearchReplaceActionPerformed(evt);
            }
        });
    }
    
	private void jMenuEditPreferencesActionPerformed(ActionEvent evt) {
		JFrame viewforPreferences= new Preferences();
	}
	protected void jMenuEditUndoActionPerformed(ActionEvent evt) {
		mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea().undoLastAction();
//		mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea().undoLastAction();		
	}

	protected void jMenuEditPasteActionPerformed(ActionEvent evt) {
		mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea().paste();
//		mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea().paste();
		
	}

	protected void jMenuEditCopyActionPerformed(ActionEvent evt) {
		mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea().copy();
//		mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea().copy();
		
	}

	protected void jMenuEditCutActionPerformed(ActionEvent evt) {
		mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea().cut();
//		mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea().cut();
		
	}

	protected void jMenuEditRedoActionPerformed(ActionEvent evt) {
		mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea().redoLastAction();
//		mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea().redoLastAction();
	}

    public void setController(IMenuEditController c){
    	mController = c;
    }
    
	private void jMenuFileSearchReplaceActionPerformed(ActionEvent evt) {
		RSyntaxTextArea text = mController.getMainController().getEditorController().getControllerOfFocusedTab().getTextArea();
//		RSyntaxTextArea text = mController.getMainController().getEditorController().getTextEditorController().getRSyntaxTextArea();
		JFrame viewForFindReplace = new SearchText(text);
	}	
}
